/**
 * implements hiding of units beyond visual range
 */
package main;

public class FogOfWar {

	boolean[][] data;
	BasicMap map;
	UnitArray units;
	Player owner;

	/**
	 * constructor
	 * @param o - owner player
	 */
	public FogOfWar(Player o){
		owner=o;
		map=o.getBm();
		units=o.getUnits();
		data= new boolean[map.getWidth()][map.getHeight()];
		incrementTurn();
	}

	/**
	 * updates data boolean array on each turn to handle updated positions
	 */
	public void incrementTurn(){//basic vision distance set to 5
		for(int i=0; i<data.length; i++)
			for(int j=0; j<data[0].length; j++)
				data[i][j]=false;
		for(Unit A: units.getArray()){		
			for(int i=0; i<=A.getMove();i++){//set the basic values of the 2D array for top half.
				for(int j=-i;j<=i;j++)//from -i to i, sort of like l in quantum orbitals.
				{
					int ycheck=A.getY()-A.getMove()+i; //identifies the y position of the point you're looking at
					int xcheck=A.getX()+j;//identifies the x position of the point you're looking at.
					if(xcheck<map.getWidth() && ycheck<map.getHeight() && xcheck>=0 && ycheck>=0){
						data[xcheck][ycheck]=true;
					}
				}
			}
			for(int i=A.getMove()-1; i>=0;i--){//set the basic values of the 2D array for bottom half.
				for(int j=-i;j<=i;j++)//from -i to i, sort of like l in quantum orbitals.
				{
					int ycheck=A.getY()+A.getMove()-i; //identifies the y position of the point you're looking at
					int xcheck=A.getX()+j;//identifies the x position of the point you're looking at.
					if(xcheck<map.getWidth() && ycheck<map.getHeight() && xcheck>=0 && ycheck>=0){
						data[xcheck][ycheck]=true;
					}
				}
			}

		}
		if(owner.getCity().getLocation()[0]+1>=0 && owner.getCity().getLocation()[1]>=0 &&owner.getCity().getLocation()[1]<map.getHeight()&&owner.getCity().getLocation()[0]+1<map.getWidth())
			data[owner.getCity().getLocation()[0]+1][owner.getCity().getLocation()[1]]=true;
		if(owner.getCity().getLocation()[0]-1>=0 && owner.getCity().getLocation()[1]>=0 &&owner.getCity().getLocation()[1]<map.getHeight()&&owner.getCity().getLocation()[0]-1<map.getWidth())
			data[owner.getCity().getLocation()[0]-1][owner.getCity().getLocation()[1]]=true;
		if(owner.getCity().getLocation()[0]>=0 && owner.getCity().getLocation()[1]-1>=0 &&owner.getCity().getLocation()[1]-1<map.getHeight()&&owner.getCity().getLocation()[0]-1<map.getWidth())
			data[owner.getCity().getLocation()[0]][owner.getCity().getLocation()[1]-1]=true;
		if(owner.getCity().getLocation()[0]>=0 && owner.getCity().getLocation()[1]+1>=0 &&owner.getCity().getLocation()[1]+1<map.getHeight()&&owner.getCity().getLocation()[0]-1<map.getWidth())
			data[owner.getCity().getLocation()[0]][owner.getCity().getLocation()[1]+1]=true;
	}
}

